package com.eateateat.controller;



import com.eateateat.service.MenuService;
import com.eateateat.service.SellerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;


@RestController  //==@Controller+@ResponseBody  所有返回以json格式
@RequestMapping("/seller")
public class SellerController {
	Logger logger = Logger.getLogger(SellerController.class);

//@Before("aop()")
@GetMapping("/login")//登录hash表返回两个数据
String slogin(HttpServletRequest request, @RequestParam Map map) throws IOException {
	logger.debug(map);
	Map m=sellerService.slogin(map);
	logger.debug(m);
	m.put("usertype",1);//商家
	logger.debug(m);
	if (m!=null) {
//		request.getSession().setAttribute("selllogin",m);
		request.getSession().setAttribute("islogin",m);
		return  "success";
	} else {
//		request.getSession().setAttribute("selllogin", "mtc_null");
		request.getSession().setAttribute("islogin", null);
		return ("failure");
	}
}
	@Autowired
	SellerService sellerService;
	@Autowired
	MenuService menuService2;

	@GetMapping("/check")
	void check(HttpServletResponse response,HttpServletRequest req) throws IOException {
		String lxname = req.getParameter("lxname");
		logger.debug(lxname);
		PrintWriter out= response.getWriter();
		if(sellerService.checkname(lxname)){
			out.print("have");
		}else {
			out.print("meiyou");
		}

	}

	@PostMapping("/register")
	String sregister(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map) throws IOException {
	logger.debug(map);
//		Map	islogin= (Map) request.getSession().getAttribute("islogin");
//	map.putAll(islogin);
		if (sellerService.sregister(map)) {
//			logger.debug(map);

//			response.sendRedirect(request.getServletContext().getContextPath() + "/slogin.html");//跳转
			return "success";
		} else {
//			response.sendRedirect(request.getServletContext().getContextPath() + "/sregister.html");//跳转
			return "failure";
		}
	}

	@GetMapping("/islogin")
	String islogin(HttpServletRequest req,HttpServletResponse resp,@RequestParam Map map) throws IOException {
		logger.debug(map);
//		Map islogin=(Map) req.getSession().getAttribute("selllogin");
		Map islogin=(Map) req.getSession().getAttribute("islogin");

		logger.debug(map);

		if(islogin==null) {
//			islogin.equals("mtc_null");
			//islogin = "mtc_null";
			return "mtc_null";
		}
		logger.debug(islogin);//{lxid=4, lxtel=null, lxpass=Yi123456, lxaddr=1, lxname=哒玲玲}
		return (islogin.toString());//{{=}}

	}

	@PostMapping("/select")
	List select(@RequestParam Map map, HttpServletRequest request)throws IOException{
		logger.debug(map);
		Map m= (Map) request.getSession().getAttribute("selllogin");
		logger.debug(m);
		map.put("lxsellid",m.get("lxid"));
		logger.debug(map);
		List lst= menuService2.getMenu(map);
		logger.debug("insert success");
		return lst;
	}

}
